package com.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.feignclient.IServiceB;

@RestController
@RequestMapping("/api-b")
public class ServiceBContoller {

	@Autowired
	private IServiceB provider;

	@GetMapping("/")
	public String init(HttpServletRequest request) {
		return "service b " + request.getLocalAddr() + "," + request.getLocalPort();
	}

	@GetMapping("/test")
	public String getATest(HttpServletRequest request) {
		return "service b test() " + request.getLocalAddr() + "," + request.getLocalPort();
	}

	@GetMapping("/port")
	public String getProt() {
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();
		ServletRequestAttributes sra = (ServletRequestAttributes) ra;
		HttpServletRequest request = sra.getRequest();
		return request.getLocalAddr() + ":" + request.getLocalPort();
	}

	@GetMapping("/call-service-a")
	public String getInfo() {
		return provider.apiInfo();
	}
}
